
class Storage {
  constructor(key) {
    this.key = key
    this.storage = window?.utools?.dbStorage.getItem(key) || []
  }

  put(data){
    this.upStorage()
    let bool = this.storage.find(item=>{
      return item.nanoid === data.nanoid
    })
    if (bool) {
      this.delete(data.nanoid)
      this.storage.push(data)
      window.utools.dbStorage.setItem(this.key, this.storage)
    }else{
      this.storage.push(data)
      window.utools.dbStorage.setItem(this.key, this.storage)
    }
  }

  upStorage(){
    this.storage = window.utools.dbStorage.getItem(this.key) || []
  }

  delete(nanoid) {
    this.upStorage();

    let index = this.storage.findIndex(item => {
      return item.nanoid === nanoid
    })

    if (index > -1) {
      this.storage.splice(index, 1)
      window.utools.dbStorage.setItem(this.key, this.storage)
    }
  }

}

const pendantStorage = ()=>{
  const setStorage = ({url, nanoid, data}) => {
    // let storage = getStorage(url);
    // storage[nanoid] = data ;
    // window.utools.dbStorage.setItem(url, storage);
    window.utools.dbStorage.setItem(nanoid, data);
  }

  const getStorage = (key) => {
    return window.utools.dbStorage.getItem(key) || '';
  }

  const getItemStorage = (url, nanoid) => {
    let storage = getStorage(url);
    return storage[nanoid] || ''
  }

  const deleteItemStorage = (storageKey, id) => {
    // let storage = getStorage(storageKey);
    // if(storage[id]){
    //   delete storage[id];
    //   window.utools.dbStorage.setItem(storageKey, storage);
    // }
    window.utools.dbStorage.removeItem(id)

  }


  return { setStorage,getStorage, getItemStorage, deleteItemStorage }
}


// 需在 preload 中导入
/*
const { readFile } = require('fs/promises')
const buffer = require("node:buffer")
const path = require('path')
 */
const pathParse = (filePath) => {
  let file = window.path.parse(filePath);
  let ext = file.ext.slice(1)
  return [file.name, ext]
}

const saveBaseImg = async (urls, regex) => {
  let buffer64s = [];
  for(let index in urls){
    let item = urls[index];
    let url = item.split(regex).at(-1);
    let [fileName, extension] = pathParse(url);
    let imageBuffer = await window.readFile(url);
    const base64Image = imageBuffer.toString('base64');
    window.utools.db.postAttachment(item, imageBuffer, `image/${extension}`);
    
    buffer64s.push({
      base64Image,
      name: fileName,
      extension: `image/${extension}`,
      url: item
    })
  }
  return buffer64s;
}


const getBaseImg = (urls) => {
  return urls.map((item)=>{
    let imageBuffer = window.utools.db.getAttachment(item);
    let extension = window.utools.db.getAttachmentType(item);
    let bufferred = window.buffer.Buffer.from(imageBuffer);
    let base64Image = bufferred.toString('base64');

    return {
      base64Image,
      extension
    }
  })
}

const removeBaseImg = (urls) => {
  urls.forEach(item=>{
    window.utools.dbStorage.removeItem(item)
  })
}


const storage = new Storage('window_components');
// exports.pendantStorage = pendantStorage;
// exports.saveBaseImg = saveBaseImg;
// exports.getBaseImg = getBaseImg;
// exports.removeBaseImg = removeBaseImg;

export { storage, pendantStorage, saveBaseImg, getBaseImg, removeBaseImg}